//ST*****************************************数据处理***************************************
function RxData(data) {
	this.keys = [];
	if (data) {
		this.toSelf(data);
	}
}

RxData.prototype = {
	toSelf: function(data) {
		if (!data) {
			return;
		}
		for (var key in data) {
			var subData = data[key];
			if (subData == undefined || subData == null) {
				this.set(key, null);
			} else if (RxCommon.isObject(subData)) {
				this.set(key, new RxData(subData));
			} else if (RxCommon.isArray(subData)) {
				this.set(key, this._arrayTo(subData));
			} else {
				this.set(key, subData);
			}
		}
	},
	_arrayTo: function(data) {
		var rxdata = [];
		for (var i = 0; i < data.length; i++) {
			if (RxCommon.isObject(data[i])) {
				rxdata[i] = new RxData(data[i]);
			} else if (RxCommon.isArray(data[i])) {
				rxdata[i] = this._arrayTo(data[i]);
			} else {
				rxdata[i] = data[i];
			}
		}
		return rxdata;
	},
	toJson: function() {
		var data = null;
		if (!this.isEmpty()) {
			data = {};
			for (var i = 0; i < this.keys.length; i++) {
				var prop = this.get(this.keys[i]);
				if (prop == undefined || prop == null) {
					continue;
				}
				if (RxCommon.isObject(prop)) {
					data[this.keys[i]] = prop.toJson();
				} else if (RxCommon.isArray(prop)) {
					data[this.keys[i]] = this._toArray(prop);
				}else{
					data[this.keys[i]] = this.get(this.keys[i]);
				}
			}
		}
		return data;
	},
	_toArray: function(data) {
		var rxdata = [];
		for (var i = 0; i < data.length; i++) {
			if (RxCommon.isObject(data[i])) {
				rxdata[i] = data[i].toJson();
			} else if (RxCommon.isArray(data[i])) {
				rxdata[i] = this._toArray(data[i]);
			} else {
				rxdata[i] = data[i];
			}
		}
		return rxdata;
	},
	isEmpty: function(key) {
		if (key) {
			var prop = this.get(key);
			if (prop == undefined || prop == null) {
				return true;
			} else if (RxCommon.isObject(prop)) {
				if (!prop.isEmpty()) {
					return false;
				}
			} else if (RxCommon.isArray(prop)) {
				if (prop.length > 0) {
					return false;
				}
			} else {
				if (!RxCommon.isEmpty(prop)) {
					return false;
				}
			}
		} else {
			for (var i = 0; i < this.keys.length; i++) {
				var prop = this.get(this.keys[i]);
				if (prop == undefined || prop == null) {
					continue;
				}
				if (RxCommon.isObject(prop)) {
					if (!prop.isEmpty()) {
						return false;
					}
				} else if (RxCommon.isArray(prop)) {
					if (prop.length > 0) {
						return false;
					}
				} else {
					//2018-01-18 by lxg
					if (prop == "" || !RxCommon.isEmpty(prop)) {
						return false;
					}
				}
			}
		}
		return true;
	},
	isOnly: function(keys) {
		if (this.keys.length != keys.length) {
			return false;
		}
		for (var i = 0; i < keys.length; i++) {
			var isFind = false;
			for (var j = 0; j < this.keys.length; j++) {
				if (keys[i] === this.keys[j]) {
					isFind = true;
				}
			}
			if (!isFind) {
				return false;
			}
		}
		return true;
	},
	clone: function() {
		var rxdata = new RxData();
		for (var i = 0; i < this.keys.length; i++) {
			var prop = this.get(this.keys[i]);
			if (RxCommon.isObject(prop)) {
				var propClone = prop.clone();
				rxdata.set(this.keys[i], propClone);
			} else if (RxCommon.isArray(prop)) {
				rxdata.set(this.keys[i], this._cloneArray(prop));
			} else {
				rxdata.set(this.keys[i], prop);
			}
		}
		return rxdata;
	},
	_cloneArray: function(data) {
		var rxdata = [];
		for (var i = 0; i < data.length; i++) {
			if (RxCommon.isObject(data[i])) {
				rxdata[i] = data[i].clone();
			} else if (RxCommon.isArray(data[i])) {
				rxdata[i] = this._cloneArray(data[i]);
			} else {
				rxdata[i] = data[i];
			}
		}
		return rxdata;
	},
	merge: function(rxdata) {
		if (!rxdata) {
			return;
		}
		for (var i = 0; i < rxdata.keys.length; i++) {
			var propCopy = rxdata[rxdata.keys[i]];
			if (RxCommon.isObject(propCopy)) {
				var prop = this.get(rxdata.keys[i]);
				if (prop == undefined || prop == null || !RxCommon.isObject(prop)) {
					this.set(rxdata.keys[i], propCopy.clone());
				} else {
					prop.merge(propCopy);
				}
			} else if (RxCommon.isArray(propCopy)) {
				this.set(rxdata.keys[i], this._cloneArray(propCopy));
			} else {
				this.set(rxdata.keys[i], propCopy);
			}
		}
	},
	removeByKeys: function(keys) {
		var data = this.clone();
		if (!keys || keys.length <= 0) {
			return data;
		}
		for (var i = 0; i < keys.length; i++) {
			data.set(keys[i], null);
		}
		return data;
	},
	remove: function(rxdatain) {
		var rxdata = new RxData();
		for (var i = 0; i < this.keys.length; i++) {
			var prop = this.get(this.keys[i]);
			var propin = rxdatain ? rxdatain.get(this.keys[i]) : null;
			if (prop == undefined || prop == null) {
				continue;
			}
			if (propin == undefined || propin == null) {
				rxdata.set(this.keys[i], prop);
			} else if (RxCommon.isObject(prop)) {
				var subData = prop.remove(propin);
				if (!subData.isEmpty()) {
					rxdata.set(this.keys[i], subData);
				}
			} else if (RxCommon.isArray(prop)) {
				var subDatas = this._removeArray(prop, propin);
				if (subDatas != null && subDatas.length > 0) {
					rxdata.set(this.keys[i], subDatas);
				}
			} else {
				if (prop !== propin) {
					rxdata.set(this.keys[i], prop);
				}
			}
		}
		return rxdata;
	},
	_removeArray: function(prop, propin) {
		var data = [];
		var isSame = true;
		for (var i = 0; i < prop.length; i++) {
			if (propin[i] == undefined || propin[i] == null) {
				if (RxCommon.isObject(prop[i])) {
					data[i] = prop[i].clone();
				} else if (RxCommon.isArray(prop[i])) {
					data[i] = this._cloneArray(prop[i]);
				} else {
					data[i] = prop[i];
				}
			} else {
				if (RxCommon.isObject(prop[i])) {
					data[i] = prop[i].remove(propin[i]);
				} else if (RxCommon.isArray(prop[i])) {
					data[i] = this._removeArray(prop[i], propin[i]);
				} else {
					data[i] = prop[i];
				}
			}
			if (data[i] != null && (!data[i].isEmpty() || data[i].length > 0)) {
				isSame = false;
			}
		}
		if (!isSame) {
			return data;
		} else {
			return null;
		}
	},
	getSame: function(rxdata) {
		var rxsame = new RxData();
		if (rxdata) {
			for (var i = 0; i < this.keys.length; i++) {
				var prop = this.get(this.keys[i]);
				var propin = rxdata.get(this.keys[i]);
				if (prop == undefined || prop == null 
				 || propin == undefined || propin == null) {
					continue;
				}
				if (RxCommon.isObject(prop) && RxCommon.isObject(propin)) {// lxg 2017-10-26
					var subData = prop.getSame(propin);
					if (!subData.isEmpty()) {
						rxsame.set(this.keys[i], subData);
					}
				} else if (RxCommon.isArray(prop)) {
					if (this._equalsArray(prop, propin)) {
						rxsame.set(this.keys[i], this._cloneArray(prop));
					}
				} else {
					if (prop === propin) {
						rxsame.set(this.keys[i], prop);
					}
				}
			}
		}
		return rxsame;
	},
	clear: function() {
		var self = new RxData();
		self.toSelf(this.toJson());
		return self;
	},
	clearValue: function(keys, value) {
		for (var i = 0; i < keys.length; i++) {
			if (this.get(keys[i])) {
				this[keys[i]] = value;
			}
		}
	},
	set: function(key, value) {
		if (!this.hasOwnProperty(key)) {
			this.keys[this.keys.length] = key;
		}
		this[key] = value;
	},
	get: function(key) {
		return this[key];
	},
	setValue: function(key, value) {
		if (value === undefined) {
			return;
		}
		var keys = key.split(".");
		var self = this;
		for (var j = 0; j < keys.length; j++) {
			if (j < keys.length - 1) {
				self = self[keys[j]];
			} else {
				self.set(keys[j], value);
			}
		}
	},
	getValue: function(key) {
		if (RxCommon.isEmpty(key)) {
			return null;
		}
		var keys = key.split(".");
		var self = this;
		for (var j = 0; j < keys.length; j++) {
			if (j < keys.length - 1) {
				self = self[keys[j]];
				if (self === undefined) {
					return null;
				}
			} else {
				value = self.get(keys[j]);
			}
		}
		return value;
	},
	equals: function(rxdata) {
		if (!rxdata) {
			return false;
		}
		if (this.keys.length != rxdata.keys.length) {
			return false;
		}
		for (var i = 0; i < this.keys.length; i++) {
			var prop = this[this.keys[i]];
			if (RxCommon.isObject(prop)) {
				return prop.equals(rxdata[this.keys[i]]);
			} else if (RxCommon.isArray(prop)) {
				return this._equalsArray(prop, rxdata[this.keys[i]]);
			} else {
				if (prop !== rxdata[this.keys[i]]) {
					return false;
				}
			}
		}
		return true;
	},
	_equalsArray: function(prop, propin) {
		if (prop == undefined || prop == null 
		 || propin == undefined || propin == null) {
			return false;
		}
		if (!RxCommon.isArray(propin)) {
			return false;
		}
		if (prop.length != propin.length) {
			return false;
		}
		for (var i = 0; i < prop.length; i++) {
			if (RxCommon.isObject(prop[i])) {
				return prop[i].equals(propin[i]);
			} else if (RxCommon.isArray(prop[i])) {
				return this._equalsArray(prop[i], propin[i]);
			} else if (prop[i] != propin[i]) {
				return false;
			}
		}
		return true;
	}
};
//ED*****************************************数据处理***************************************
